﻿using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Xml;
using System.Diagnostics.Contracts;

namespace Landscape {
    static class StringExtensions {

        public static XmlElement ToXmlNode(this string s, ref XmlDocument document, IDictionary<string, object> mapAttributes = null) {
            Contract.Requires(document != null);
            XmlDocumentFragment frag = document.CreateDocumentFragment();
            frag.InnerXml = s;

            Contract.Assert(frag.FirstChild != null, "Tag is not well formed");

            XmlElement node = (XmlElement)frag.FirstChild;

            if (mapAttributes != null) {
                foreach (KeyValuePair<string, object> row in mapAttributes) {
                    node.SetAttribute(row.Key, row.Value.ToString());
                }
            }

            return node;
        }
    }
}
